#!/bin/bash

# 测试结果管理脚本
# 用于查看和管理历史测试结果

RESULTS_BASE_DIR="$(pwd)/results"

# 颜色输出
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

show_help() {
    echo "测试结果管理工具"
    echo ""
    echo "用法:"
    echo "  $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -l, --list          列出所有测试会话"
    echo "  -s, --show <ID>     显示指定会话的报告"
    echo "  -c, --compare       对比最近两次测试结果"
    echo "  -d, --delete <ID>   删除指定会话"
    echo "  -h, --help          显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 -l                    # 列出所有测试"
    echo "  $0 -s 20250708_172030    # 显示指定测试报告"
    echo "  $0 -c                    # 对比最近两次测试"
}

list_sessions() {
    echo -e "${GREEN}历史测试会话列表:${NC}"
    echo "----------------------------------------"
    
    if [ ! -d "$RESULTS_BASE_DIR" ]; then
        echo "没有找到测试结果目录"
        return 1
    fi
    
    sessions=($(ls -1 "$RESULTS_BASE_DIR" | grep -E "^[0-9]{8}_[0-9]{6}$" | sort -r))
    
    if [ ${#sessions[@]} -eq 0 ]; then
        echo "没有找到测试会话"
        return 1
    fi
    
    for session in "${sessions[@]}"; do
        session_dir="$RESULTS_BASE_DIR/$session"
        if [ -f "$session_dir/analysis_report.txt" ]; then
            # 提取报告生成时间
            gen_time=$(grep "生成时间:" "$session_dir/analysis_report.txt" | cut -d: -f2- | xargs)
            echo -e "${BLUE}会话ID:${NC} $session"
            echo -e "${YELLOW}时间:${NC} $gen_time"
            
            # 显示简要统计
            if [ -f "$session_dir/summary.csv" ]; then
                total_tests=$(wc -l < "$session_dir/summary.csv")
                successful_tests=$(awk -F',' '$3 != "" && $4 != "" && $5 != ""' "$session_dir/summary.csv" | wc -l)
                echo -e "${YELLOW}测试数:${NC} $total_tests (成功: $successful_tests)"
            fi
            echo "----------------------------------------"
        fi
    done
}

show_session() {
    local session_id="$1"
    if [ -z "$session_id" ]; then
        echo "错误: 请指定会话ID"
        return 1
    fi
    
    local session_dir="$RESULTS_BASE_DIR/$session_id"
    if [ ! -d "$session_dir" ]; then
        echo "错误: 会话 $session_id 不存在"
        return 1
    fi
    
    if [ -f "$session_dir/analysis_report.txt" ]; then
        echo -e "${GREEN}=== 测试会话 $session_id 报告 ===${NC}"
        cat "$session_dir/analysis_report.txt"
    else
        echo "错误: 报告文件不存在"
        return 1
    fi
}

compare_sessions() {
    sessions=($(ls -1 "$RESULTS_BASE_DIR" | grep -E "^[0-9]{8}_[0-9]{6}$" | sort -r | head -2))
    
    if [ ${#sessions[@]} -lt 2 ]; then
        echo "错误: 需要至少两个测试会话才能进行对比"
        return 1
    fi
    
    echo -e "${GREEN}=== 对比最近两次测试结果 ===${NC}"
    echo -e "${BLUE}较新测试:${NC} ${sessions[0]}"
    echo -e "${BLUE}较旧测试:${NC} ${sessions[1]}"
    echo ""
    
    # 对比CSV数据
    newer_csv="$RESULTS_BASE_DIR/${sessions[0]}/summary.csv"
    older_csv="$RESULTS_BASE_DIR/${sessions[1]}/summary.csv"
    
    if [ -f "$newer_csv" ] && [ -f "$older_csv" ]; then
        echo "性能对比 (连接数 -> SQL执行率):"
        echo "连接数    较新测试    较旧测试    变化"
        echo "----------------------------------------"
        
        # 简单的性能对比逻辑
        while IFS=',' read -r conn1 dur1 active1 sql1 success1; do
            if [ -n "$sql1" ] && [ -n "$active1" ]; then
                # 在旧数据中查找相同连接数的测试
                older_data=$(grep "^$conn1," "$older_csv" | head -1)
                if [ -n "$older_data" ]; then
                    older_sql=$(echo "$older_data" | cut -d',' -f4)
                    if [ -n "$older_sql" ]; then
                        change=$(echo "scale=2; $sql1 - $older_sql" | bc 2>/dev/null || echo "N/A")
                        printf "%-8s  %-10s  %-10s  %s\n" "$conn1" "$sql1" "$older_sql" "$change"
                    fi
                fi
            fi
        done < "$newer_csv"
    fi
}

delete_session() {
    local session_id="$1"
    if [ -z "$session_id" ]; then
        echo "错误: 请指定会话ID"
        return 1
    fi
    
    local session_dir="$RESULTS_BASE_DIR/$session_id"
    if [ ! -d "$session_dir" ]; then
        echo "错误: 会话 $session_id 不存在"
        return 1
    fi
    
    echo -e "${YELLOW}确认删除测试会话 $session_id? (y/N)${NC}"
    read -r confirm
    if [[ "$confirm" =~ ^[Yy]$ ]]; then
        rm -rf "$session_dir"
        echo -e "${GREEN}会话 $session_id 已删除${NC}"
    else
        echo "取消删除"
    fi
}

# 参数解析
case "$1" in
    -l|--list)
        list_sessions
        ;;
    -s|--show)
        show_session "$2"
        ;;
    -c|--compare)
        compare_sessions
        ;;
    -d|--delete)
        delete_session "$2"
        ;;
    -h|--help|"")
        show_help
        ;;
    *)
        echo "未知参数: $1"
        show_help
        exit 1
        ;;
esac
